﻿using System.Collections.Generic;
using System.Linq;
using Jay.Sc2.Bof.Lib.Action;

namespace Jay.Sc2.Bof.Lib.State
{
	public class BuildOrder : IBuildOrder
	{
		public BuildOrder(IRace race, IState initial, IGoal goal, IActionList actionList)
		{
			m_Race = race;
			m_State = initial;
			m_Initial = initial;
			m_Goal = goal;
			m_ActionList = actionList;
		}
		private SortedDictionary<int, List<IAction>> m_ActionsByTime = new SortedDictionary<int, List<IAction>>();

		private IRace m_Race;
		public IRace Race { get { return m_Race; } }
		private IState m_State;
		public IState State { get { return m_State; } }
		private IState m_Initial;
		public IState Initial { get { return m_Initial; } }
		private IGoal m_Goal;
		public IGoal Goals { get { return m_Goal; } }
		private IActionList m_ActionList;
		public IActionList ActionList { get { return m_ActionList; } }
		private double m_Score;
		public double Score { get { return m_Score; } set { m_Score = value; } }

		public int LastActionTime { get { return m_ActionsByTime.Count() == 0 ? 0 : m_ActionsByTime.Last().Key; } }
		public bool IsOver { get { return m_ActionsByTime.Count() == 0 || State.Time >= m_ActionsByTime.Last().Key; } }
		public ICollection<IAction> GetActions(int time)
		{
			List<IAction> actions;
			if (m_ActionsByTime.TryGetValue(time, out actions))
				return actions;
			return null;
		}
		public void AddAction(int time, IAction action)
		{
			List<IAction> actions;
			if (!m_ActionsByTime.TryGetValue(time, out actions))
			{
				actions = new List<IAction>();
				m_ActionsByTime.Add(time, actions);
			}
			actions.Add(action);
		}

	}
}
